﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using PlaytimeSaveTypes;

namespace BoneAnimationProt
{
    public static class TypeBuilder
    {
        public static JointSaveData CreateJointSaveData(Joint _joint)
        {
            JointSaveData outp = new JointSaveData();
            outp.data.relativeRotation = _joint.relativeRotation;
            outp.data.relativeTranslation = _joint.relativeTranslation;
            outp.data.Name = _joint.Name;
            outp.ParentName = (_joint.parent != null ? _joint.parent.Name : "");
            if (_joint.sprite.HasValue)
                outp.sprite = CreateSpriteSaveData(_joint.sprite);
            return outp;
        }

        public static SpriteSaveData CreateSpriteSaveData(Sprite? _sprite)
        {
            if (_sprite.HasValue)
            {
                SpriteSaveData outp = new SpriteSaveData();
                outp.TextureName = _sprite.Value.Name;
                outp.Scale = _sprite.Value.Effects == Microsoft.Xna.Framework.Graphics.SpriteEffects.None ? Vector2.One : new Vector2(-1,0);
                outp.Offset = _sprite.Value.Offset;
                outp.rotOffset = _sprite.Value.rotOffset;
                outp.depth = _sprite.Value.depth;
                return outp;
            }
            return null;
        }

        public static JointAnimationSaveData CreateAnimationSaveData(JointAnimation _anim)
        {
            JointAnimationSaveData outp = new JointAnimationSaveData();
            outp.animationLength = _anim.animationLength;
            outp.animationSpeed = _anim.animationSpeed;

            outp.keyframes = new SerializableDictionary<float, List<JointData>>();
            outp.joints = new List<JointSaveData>();
            foreach (Joint joint in _anim.joints)
            {
                outp.joints.Add(CreateJointSaveData(joint));
            }
            foreach (KeyValuePair<float, List<JointData>> keyframe in _anim.keyframes)
                outp.keyframes.Add(keyframe.Key, keyframe.Value);
            return outp;
        }
    }
}
